﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using System.Drawing;
using System.Drawing.Imaging;
using BenzInfo.Hockey.Grenadiers.Utils;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{ 
    public class TeamsController : BaseController
    {
        private GrenadiersContext db = new GrenadiersContext();

        private const string BASE_PATH_TEAM = "~/Content/images/teams";
        private const string BASE_PATH_LOGO = "~/Content/images/logos";

        //
        // GET: /Teams/

        public ViewResult Index()
        {
            return View(db.Teams.ToList());
        }

        //
        // GET: /Teams/Create

        public ActionResult Create()
        {
            return PartialView();
        } 

        //
        // POST: /Teams/Create

        [HttpPost]
        public ActionResult Create(Team team, HttpPostedFileBase logoFile, HttpPostedFileBase teamFile)
        {
            if (logoFile != null)
            {
                using (Image img = Image.FromStream(logoFile.InputStream))
                {
                    string ext = logoFile.FileName.Split('.')[logoFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, team.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH_LOGO), filename);

                    ImageHelper.Save(img, filepath, ext);

                    team.Logo = filename;
                }
            }

            if (teamFile != null)
            {
                using (Image img = Image.FromStream(teamFile.InputStream))
                {
                    string ext = teamFile.FileName.Split('.')[teamFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, team.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH_TEAM), filename);

                    ImageHelper.Save(img, filepath, ext);

                    team.Picture = filename;
                }
            }

            if (ModelState.IsValid)
            {
                db.Teams.Add(team);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(team);
        }
        
        //
        // GET: /Teams/Edit/5
 
        public ActionResult Edit(int id)
        {
            Team team = db.Teams.Find(id);
            return PartialView(team);
        }

        //
        // POST: /Teams/Edit/5

        [HttpPost]
        public ActionResult Edit(Team team, HttpPostedFileBase logoFile, string nologo, HttpPostedFileBase teamFile, string nopicture)
        {
            if (nologo == "on")
            {
                team.Logo = null;
            }
            else if (logoFile != null)
            {
                using (Image img = Image.FromStream(logoFile.InputStream))
                {
                    string ext = logoFile.FileName.Split('.')[logoFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, team.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH_LOGO), filename);

                    ImageHelper.Save(img, filepath, ext);

                    team.Logo = filename;
                }
            }

            if (nopicture == "on")
            {
                team.Picture = null;
            }
            else if (teamFile != null)
            {
                using (Image img = Image.FromStream(teamFile.InputStream))
                {
                    string ext = teamFile.FileName.Split('.')[teamFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, team.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH_TEAM), filename);

                    ImageHelper.Save(img, filepath, ext);

                    team.Picture = filename;
                }
            }

            if (ModelState.IsValid)
            {
                db.Entry(team).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return PartialView(team);
        }

        //
        // GET: /Teams/Delete/5
 
        public ActionResult Delete(int id)
        {
            Team team = db.Teams.Find(id);
            return PartialView(team);
        }

        //
        // POST: /Teams/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Team team = db.Teams.Find(id);
            db.Teams.Remove(team);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}